package com.kk.netty.nio.file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class TestFileChannel {
    public static void main(String[] args) {
        String FROM = "boot_netty/helloword/data.txt";
        String TO = "boot_netty/helloword/to.txt";
        long start = System.nanoTime ( );
        try (
                FileChannel from = new FileInputStream (FROM).getChannel ( );
                FileChannel to = new FileOutputStream (TO).getChannel ( );
        ) {
            // 数据 from 传输到  to*（就是一个拷贝过程）
            // 效率会比传统的原生api 高，底层会用操作系统的零拷贝进行优化
            long size = from.size ( );
            // left 变量代表还剩余多少字节
            for (long left = size; left > 0; ) {
                System.out.println ("position:" + (size - left) + " left:" + left);
                left -= from.transferTo (size - left, left, to);
            }
        } catch (IOException e) {
            e.printStackTrace ( );
        }
        long end = System.nanoTime ( );
        System.out.println ("transferTo 用时：" + (end - start) / 1000_000.0);
    }
}
